package com.education.teach.mvp.presenter;

import android.app.Application;

import com.education.teach.mvp.model.bean.CounselBean;
import com.education.teach.mvp.model.bean.CounselDetailBean;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.education.teach.mvp.contract.CounselDetailContract;
import com.jess.arms.utils.RxLifecycleUtils;

import java.util.HashMap;
import java.util.Map;


@ActivityScope
public class CounselDetailPresenter extends BasePresenter<CounselDetailContract.Model, CounselDetailContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public CounselDetailPresenter(CounselDetailContract.Model model, CounselDetailContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public void saveConsultationQuestion(String xlh, String content, String id, String name) {
        Map<String, String> map = new HashMap<>();
        map.put("yhxlh", xlh);
        map.put("content", content);
        map.put("consultationId", id);
        map.put("yhmc", name);

        mModel.saveConsultationQuestion(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .doOnSubscribe(disposable -> mRootView.showLoading())
                .doFinally(() -> mRootView.hideLoading())
                .subscribe(new ErrorHandleSubscriber<CounselDetailBean>(mErrorHandler) {
                    @Override
                    public void onNext(CounselDetailBean counselBean) {
                        mRootView.retrunSaveResult(counselBean);
                    }
                });

    }
}
